package cn.edu.scau.cmi.chenmiaopeng.controller.adapter;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;

import java.util.List;
import java.util.ArrayList;
import cn.edu.scau.cmi.chenmiaopeng.adapter.*;

public class AdapterController {
	@FXML
    private RadioButton objectAdapter;
    @FXML
    private TextField textfield1;
    @FXML
    private TextField textfield2;
    @FXML
    private TextArea textarea;
    @FXML
    private RadioButton classAdapter;
    @FXML
    private Button add_show;
    @FXML
    private Button check;
    
    private List<Student> stuList;
    private StudentSortClassTarget sortClassTarget; 
    private StudentSortObjectTarget sortObjectTarget; 
    
    public AdapterController() {  //在构造方法里面构造stuList，并初始化
    	sortClassTarget = new StudentSortClassAdapter();
		sortObjectTarget = new StudentSortObjectAdapter();
    	stuList = new ArrayList<Student>();
    	stuList.add(new Student(1,"王俊"));
    	stuList.add(new Student(2,"孙五"));
    	stuList.add(new Student(3,"李乐"));
    	stuList.add(new Student(4,"汤凤"));
    	sortClassTarget.sortStudent(stuList); //默认是类适配器
    }
    
    public void setTextArea(TextArea textArea) {
        textarea = textArea;	
    }
    public void addToggleListener(ToggleGroup tg) {
    	ChangeListener<Toggle> listener = new ChangeListener<Toggle>() {
            @Override
			public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
            	textfield1.clear();
            	textfield2.clear();
			}
		};
		tg.selectedToggleProperty().addListener(listener);
    }
    public void showStudentInfo() {
    	textarea.clear();
    	textarea.setText("学生信息显示如下：\n");
    	for(int i=0;i<stuList.size();i++) {
    		textarea.appendText(stuList.get(i)+"\n");
    	}
    }
    private boolean isNumberDuplicate(int stuNumber) {
    	for(int i=0;i<stuList.size();i++) {
    		if(stuList.get(i).getStuNumber()==stuNumber) {
    			return true;
    		}
    	}
    	return false;
    }
    @FXML
    public void clickButton(ActionEvent e) {
    	String stuNumberString = textfield1.getText();
        String stuName = textfield2.getText();
        Button clickButton = (Button)e.getSource();
        if(clickButton==add_show) {
        	 if(stuNumberString.isEmpty()) {
             	textarea.setText("输入学生姓名为空！\n");
             	return;
             }
             if(stuName.isEmpty()) {
             	textarea.setText("输入学生学号为空！\n");
             	return;
             }
             
             try {
             	int stuNumber = Integer.parseInt(stuNumberString);
             	if(isNumberDuplicate(stuNumber)) {
             		textarea.setText("学生学号不能重复！");
             		textfield1.clear();
                	textfield2.clear();
             	}
             	else {
             		if(classAdapter.isSelected()) {
                 		stuList.add(new Student(stuNumber,stuName));
                 		sortClassTarget.sortStudent(stuList);
                 	}
                 	else {
                 		stuList.add(new Student(stuNumber,stuName));
                 		sortObjectTarget.sortStudent(stuList);
                 	}
                     showStudentInfo();
             	}
             }
             catch(NumberFormatException error) {
             	textarea.setText("学生学号非法！请重新输入\n");
             	error.printStackTrace();
             }
        }
        else if(clickButton==check) {
        	showStudentInfo();
        }
    }
}
